package com.wye.controller;

import com.github.pagehelper.PageInfo;
import com.wye.goods.pojo.Goods;
import com.wye.goods.pojo.Spu;
import com.wye.service.SpuService;
import entity.Result;
import entity.StatusCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/spu")
@CrossOrigin
public class SpuController {

    @Autowired
    private SpuService spuService;


    /**
     * 批量上架
     */
    @PostMapping(value = "/put/many")
    public Result putMany(@RequestBody Long[] ids){
        spuService.putMany(ids);
        return new Result(true, StatusCode.OK, "上架成功");
    }

    /**
     * 上架
     */
    @PostMapping(value = "/put{id}")
    public Result put(@PathVariable(value = "id")Long spuId){
        spuService.put(spuId);
        return new Result(true, StatusCode.OK, "下架成功");
    }

    /**
     * 下架
     */
    @PostMapping(value = "/pull{id}")
    public Result pull(@PathVariable(value = "id")Long spuId){
        spuService.pull(spuId);
        return new Result(true, StatusCode.OK, "下架成功");
    }

    /**
     * 审核
     * @param spuId
     * @return
     */
    @PostMapping(value = "/audit/{id}")
    public Result audit(@PathVariable(value = "id")Long spuId){
        spuService.audit(spuId);
        return new Result(true, StatusCode.OK, "审核成功");
    }

    @GetMapping(value = "/goods/{id}")
    public Result findGoodsById(@PathVariable(value = "id")Long spuId){
        Goods goods = spuService.findGoodsById(spuId);
        return new Result(true, StatusCode.OK, "查询成功", goods);
    }

    /**
     * 查询所有品牌
     */
    @GetMapping("/findAll")
    public Result<List<Spu>> findAll(){
        List<Spu> spus = spuService.findAll();
        return new Result<List<Spu>>(true, StatusCode.OK, "查询品牌集合", spus);
    }

    @GetMapping("/findSpuById/{id}")
    public Result findSpuById(@PathVariable(value = "id")Integer id){
        Spu spu = spuService.findById(id);
        if (spu == null){
            return new Result(false, StatusCode.ERROR, "请求失败");
        }
        return new Result(true, StatusCode.OK, "请求成功", spu);
    }

    @PostMapping(value = "/add")
    public Result add(@RequestBody Spu spu){
        int resultInt = spuService.insert(spu);
        if (resultInt != 0){
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result updatespu(@RequestBody Spu spu){
            spuService.update(spu);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") Integer id){
            spuService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PostMapping(value = "/search")
    public Result<List<Spu>> search(Spu spu){
        //调用service实现查询
        if (spu != null){
            List<Spu> spus = spuService.findList(spu);
            return new Result<List<Spu>>(true, StatusCode.OK, "查询成功", spus);
        }
        return new Result<List<Spu>>(false, StatusCode.ERROR, "请输入有效参数");
    }
//
//    @RequestMapping(value = "/search/{page}/{size}")
//    public Result findPage(@PathVariable(value = "page")Integer page, //当前页
//                           @PathVariable(value = "size")Integer size,
//                           Spu spu){//每页显示的条数
//        if (spu == null){
//            PageInfo<Spu> pageInfo = spuService.findPage(page, size);
//            return new Result(true, StatusCode.OK, "分页查询成功", pageInfo);
//        }
//        PageInfo<Spu> pageInfo = SpuService.findPage(page, size, Spu);
//        return new Result(true, StatusCode.OK, "分页搜索查询成功", pageInfo);
//
//    }


}
